/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.OsDao;
import com.techshop.model.Os;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Hai-PC
 */
public class OsDaoImpl extends HibernateDaoSupport implements OsDao {

    @Override
    public Os getById(int osId) {
        try {
            return (Os) getSession().get(Os.class, osId);
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Os> getAll() {
        try {
            return getSession().createQuery("FROM Os").list();
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(Os os) {
        try {
            getHibernateTemplate().save(os);
            return true;
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean update(Os os) {
        try {
            if (getById(os.getId()) == null) {
                return false;
            } else {
                getHibernateTemplate().update(getHibernateTemplate().merge(os));
                return true;
            }
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(Os os) {
        try {
            if (getById(os.getId()) == null) {
                return false;
            } else {
                getHibernateTemplate().delete(getHibernateTemplate().merge(os));
                return true;
            }
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
}